import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import lombok.extern.log4j.Log4j2;

import java.util.concurrent.TimeUnit;

@Log4j2
public class TestEventLoop {


    public static void main(String[] args) {
        //1. 创建事件循环组
        EventLoopGroup group = new NioEventLoopGroup(2);//io 事件，普通任务，定时恩物
        EventLoopGroup group1 = new DefaultEventLoopGroup();//普通任务，定时任务
        //2. 循环获取事件对象，轮询，1和3一样
        System.out.println(group.next());//1
        System.out.println(group.next());//2
        System.out.println(group.next());//3
        //3. 执行普通任务，异步的
        group.next().submit(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            log.debug(Thread.currentThread().getName() + "普通任务1..");
        });
        log.debug("main..");
        //4. 执行定时任务
        group.next().scheduleAtFixedRate(() -> {
            log.debug(Thread.currentThread().getName() + "定时任务1..");
        }, 0, 1, TimeUnit.SECONDS);

        group.next().scheduleAtFixedRate(() -> {
            log.debug(Thread.currentThread().getName() + "定时任务2..");
        }, 0, 1, TimeUnit.SECONDS);

        group.next().submit(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            log.debug(Thread.currentThread().getName() + "普通任务2..");
        });
    }

}
